package com.yayohos.locationmanager;

import com.yayohos.locationmanager.base.LocationBaseService;
import com.yayohos.locationmanager.configuration.Configurations;
import com.yayohos.locationmanager.configuration.LocationConfiguration;
import com.yayohos.locationmanager.helper.LogUtils;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;

import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventManager;

import ohos.location.Location;

import ohos.rpc.RemoteException;


/**
 * SampleService
 */
public class SampleService extends LocationBaseService {
    /**
     * 定位成功
     */
    public static final int ACTION_LOCATION_CHANGED = 0;
    /**
     * 定位失败
     */
    public static final int ACTION_LOCATION_FAILED = 1;
    /**
     * 定位途径改变
     */
    public static final int ACTION_PROCESS_CHANGED = 2;
    /**
     * 广播EventAction
     */
    public static final String LOCATION_EVENT = "com.myLocation.event";
    /**
     * Intent Flag
     */
    public static final String EXTRA_FAIL_TYPE = "ExtraFailTypeField";
    /**
     * Intent Flag
     */
    public static final String EXTRA_PROCESS_TYPE = "ExtraProcessTypeField";

    private boolean isLocationRequested = false;

    @Override
    public LocationConfiguration getLocationConfiguration() {
        return Configurations.silentConfiguration(true);
    }

    @Override
    public void onStart(Intent intent) {
        LogUtils.logI("SampleServiceAbility::onStart");
        super.onStart(intent);
    }

    @Override
    public void onBackground() {
        super.onBackground();
        LogUtils.logI("SampleServiceAbility::onBackground");
        getLocationManager().onBackground();
    }

    @Override
    public void onStop() {
        super.onStop();
        LogUtils.logI("SampleServiceAbility::onStop");
        getLocationManager().onStop();
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
        super.onCommand(intent, restart, startId);
        LogUtils.logI("onCommand");
        getLocation();
        if (!isLocationRequested) {
            isLocationRequested = true;
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        Intent intent = new Intent();
        LogUtils.logI(location.getLatitude() + "/" + location.getLongitude());
        intent.setParam("Lat", location.getLatitude());
        intent.setParam("Lng", location.getLongitude());
        pushEvent(intent, ACTION_LOCATION_CHANGED);
        terminateAbility();
    }

    private void pushEvent(Intent intent, int code) {
        try {
            Operation operation = new Intent.OperationBuilder()
                    .withAction(LOCATION_EVENT)
                    .build();
            intent.setOperation(operation);
            intent.setParam("code", code);
            CommonEventData eventData = new CommonEventData(intent);
            LogUtils.logI("publishCommonEvent" + code + "/" + Thread.currentThread().getName());
            CommonEventManager.publishCommonEvent(eventData);
        } catch (RemoteException e) {
            LogUtils.logI("publishCommonEvent occur exception.");
        }
    }

    @Override
    public void onLocationFailed(int type) {
        Intent intent = new Intent();
        intent.setParam(EXTRA_FAIL_TYPE, type);
        pushEvent(intent, ACTION_LOCATION_FAILED);
        terminateAbility();
    }

    @Override
    public void onProcessTypeChanged(int processType) {
        LogUtils.logI("onProcessTypeChanged=" + processType);
        Intent intent = new Intent();
        intent.setParam(EXTRA_PROCESS_TYPE, processType);
        pushEvent(intent, ACTION_PROCESS_CHANGED);
    }

    @Override
    public void onDisconnect(Intent intent) {
        LogUtils.logI("onDisconnect");
        getLocationManager().onStop();
    }
}